/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yarolegovich.lovelydialog;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Checkbox;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 列表适配器
 *
 * @param <T>
 */
public class ListProvider<T> extends BaseItemProvider {
    private Context context;
    private List<T> dataList;
    private boolean isMulType = false;
    private Map<Integer, Boolean> checkMap;

    public void setMulType(boolean mulType) {
        isMulType = mulType;
    }

    public ListProvider(Context context, List items) {
        this.context = context;
        dataList = items;
        checkMap = new HashMap<>();
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        ViewHolder vh;
        if (component == null) {
            component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_simple_text, componentContainer, false);
            vh = new ViewHolder(component);
            component.setTag(vh);
        } else {
            vh = (ViewHolder) component.getTag();
        }
        String option = (String) getItem(position);
        if (option != null) {
            vh.description.setText(option);
        }
        if (isMulType) {
            vh.checkbox.setVisibility(Component.VISIBLE);
            if (vh.checkbox.isChecked()) {
                vh.checkbox.setButtonElement(ResUtil.getPixelMapDrawable(context, ResourceTable.Media_select));
            } else {
                vh.checkbox.setButtonElement(ResUtil.getPixelMapDrawable(context, ResourceTable.Media_unselect));
            }
            vh.checkbox.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    if (vh.checkbox.isChecked()) {
                        vh.checkbox.setButtonElement(ResUtil.getPixelMapDrawable(context, ResourceTable.Media_unselect));
                    } else {
                        vh.checkbox.setButtonElement(ResUtil.getPixelMapDrawable(context, ResourceTable.Media_select));
                    }
                    checkMap.put(position, !vh.checkbox.isChecked());
                }
            });
        } else {
            vh.checkbox.setVisibility(Component.HIDE);
        }
        checkMap.put(position, vh.checkbox.isChecked());
        return component;
    }

    /**
     * 选中的item
     *
     * @return map
     */
    public Map<Integer, Boolean> getCheckItem() {
        if (checkMap == null) {
            checkMap = new HashMap<>();
        }
        return checkMap;
    }

    private static final class ViewHolder {
        Text description;
        Checkbox checkbox;

        public ViewHolder(Component component) {
            description = (Text) component.findComponentById(ResourceTable.Id_item_text);
            checkbox = (Checkbox) component.findComponentById(ResourceTable.Id_item_check);
        }
    }
}
